// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   AdminSqlExecuteAction.java

package com.market.web.action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

// Referenced classes of package com.market.web.action:
//            BaseAction

public class AdminSqlExecuteAction extends BaseAction2
{

    public AdminSqlExecuteAction()
    {
    }
    public String getMode()
    {
        return mode;
    }

    public void setMode(String mode)
    {
        this.mode = mode;
    }

    public String getSql()
    {
        return sql;
    }

    public void setSql(String sql)
    {
        this.sql = sql;
    }

    private String sql;

    private String mode;
    public String execute()
        throws Exception
    {
        //SqlForm sf = (SqlForm)form;
        if(getSql() == null)
        {
            ActionMessages error = new ActionMessages();
            error.add("org.apache.struts.action.GLOBAL_MESSAGE", new ActionMessage("SQL\u8BED\u53E5\u4E0D\u80FD\u4E3A\u7A7A!", false));
            //saveErrors(request, error);
            return INPUT;//mapping.getInputForward();
        }
        String str = "";
        Connection con = getBaseService().getConnection();
        PreparedStatement pr = con.prepareStatement(getSql());
        if("select".equals(getMode()))
            str = select(con, pr);
        else
            str = update(con, pr);
        getRequest().setAttribute("result", str);
        return SUCCESS;//mapping.findForward("sql");
    }

    private String select(Connection con, PreparedStatement pr)
    {
        ResultSet rs = null;
        String s;
        try{
       
        StringBuffer str = new StringBuffer();
        rs = pr.executeQuery();
        ResultSetMetaData meta = rs.getMetaData();
        int count = meta.getColumnCount();
        str.append("<table border=\"1\" cellspacing=\"0\" bordercolor=\"#CCCCFF\">").append("<tr>");
        for(int i = 1; i <= count; i++)
            str.append("<td><strong>").append(meta.getColumnName(i)).append("</strong></td>");

        str.append("</tr>");
        for(; rs.next(); str.append("</tr>"))
        {
            str.append("<tr>");
            for(int i = 1; i <= count; i++)
            {
                Object o = rs.getObject(i);
                if(o != null)
                    str.append("<td>").append(o.toString()).append("</td>");
                else
                    str.append("<td>null</td>");
            }

        }

        str.append("</table>");
        s = str.toString();
        return s;
        }catch(Exception e)
        {
            s = "<font color=\"red\">" + e.getMessage() + "</font>";
            return s;
        }
  
        finally{
   
            try
            {
                rs.close();
                pr.close();
                con.close();
            }
            catch (SQLException e)
            {
                logger.error("",e);
            }
           
        }
    }

    private String update(Connection con, PreparedStatement pr)
    {
        String s;
        try{
        int i = pr.executeUpdate();
        s = "\u5171\u66F4\u65B0" + i + "\u884C\u6570\u636E!";
        return s;
        }catch(Exception e)
        {
            s = "<font color=\"red\">" + e.getMessage() + "</font>";
            return s;
        }
        
       finally{
       
   
            try
            {
                pr.close();
                con.close();
            }
            catch (SQLException e)
            {
                logger.error("",e);
            }
           
        }
      
       
    }
}
